package com.learning.jquizenglish.network;

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.concurrent.TimeoutException;

import android.content.Context;
import android.util.Log;
import android.view.ViewGroup;

import com.learning.jquizenglish.common.fragment.JQuizTaskFragment.JQuizTaskListener;
import com.learning.jquizenglish.model.JQuizRequestModel;
import com.learning.jquizenglish.network.connection.HttpUtil;
import com.learning.jquizenglish.network.connection.JsonUtil;

public class JQuizEnglishRequest {

	public static <T> void executeGetTask(final Context context,ViewGroup vg,final JQuizRequestModel requestModel,boolean isShowProgress,final Class<T> returnType,final JQuizTaskListener listener){
		JQuizEnglishTask<String, T> jquizTask = new JQuizEnglishTask<String, T>() {

			@Override
			public T processInBackground(String... pParams)
					throws SocketTimeoutException, TimeoutException {
				T result = null;
				try {
					final String json = HttpUtil.getStringResponseByGetMethod(pParams[0]);
					Log.i("MobiNetworkJsonTask", " Json :" + json);
					result = JsonUtil.convertObjectFromJsonString(json, returnType);
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
				return result;
			}

			@Override
			public void onUIUpDate(T result) {
				if (context !=null && listener!=null) {
					listener.onTaskComplete(result);
				}
			}
		};
		jquizTask.run(context, vg, isShowProgress, requestModel.url);
	}
	
	public static <T> void executeGetTaskReturnList(final Context context,ViewGroup vg,final JQuizRequestModel requestModel,boolean isShowProgress,final Class<T> returnType,final JQuizTaskListener listener){
		JQuizEnglishTask<String, ArrayList<T>> jquizTask = new JQuizEnglishTask<String, ArrayList<T>>() {

			@Override
			public ArrayList<T> processInBackground(String... pParams)
					throws SocketTimeoutException, TimeoutException {
				ArrayList<T> result = null;
				try {
					final String json = HttpUtil.getStringResponseByGetMethod(pParams[0]);
					Log.i("MobiNetworkJsonTask", " Json :" + json);
					result = JsonUtil.convertArrayListFromJsonString(json, returnType);
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
				return result;
			}

			@Override
			public void onUIUpDate(ArrayList<T> result) {
				if (context !=null && listener!=null) {
					listener.onTaskComplete(result);
				}
			}
		};
		jquizTask.run(context, vg, isShowProgress, requestModel.url);
	}
	
	public static <T> void executePostTask(final Context context,ViewGroup vg,final JQuizRequestModel requestModel,boolean isShowProgress,final Class<T> returnType,final JQuizTaskListener listener){
		JQuizEnglishTask<String, T> jquizTask = new JQuizEnglishTask<String, T>() {

			@Override
			public T processInBackground(String... pParams)
					throws SocketTimeoutException, TimeoutException {
				T result = null;
				try {
					String json = "";
//					final String json = HttpUtil.getStringResponeByPostMethod(
//							(String) pParams[0], requestModel.listNameValuePairs);
					if (requestModel.multipartEntity!=null) {
						json = HttpUtil.getStringResponeByPostMultipart((String)pParams[0], requestModel.multipartEntity);
					}else{
						json = HttpUtil.getStringResponeByPostMethod(
								(String) pParams[0], requestModel.listNameValuePairs);
					}
					result = JsonUtil.convertObjectFromJsonString(json, returnType);
					Log.i("MobiNetworkJsonTask", " Json :" + json);
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
				return result;
			}

			@Override
			public void onUIUpDate(T result) {
				if (context !=null && listener!=null) {
					listener.onTaskComplete(result);
				}
			}
		};
		jquizTask.run(context, vg, isShowProgress, requestModel.url);
	}
	
}
